package cn.edu.cqvie.jdk8.stream2;

import javax.swing.text.html.Option;
import java.util.*;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.*;

public class StudentTest1 {

    public static void main(String[] args) {
        Student student1 = new Student("张三", 80);
        Student student2 = new Student("李四", 90);
        Student student3 = new Student("王五", 100);
        Student student4 = new Student("赵六", 90);
        Student student5 = new Student("赵六", 90);

        List<Student> students = Arrays.asList(student1, student2, student3, student4, student5);

        List<Student> students1 = students.stream().collect(toList());
        students1.forEach(System.out::println);
        System.out.println("---------------");


        System.out.println("count:" + students.stream().collect(counting()));
        System.out.println("count:" + students.stream().count());

        System.out.println("---------------");
        students.stream().min(Comparator.comparingInt(Student::getScore))
//                .collect(minBy(Comparator.comparingInt(Student::getScore)))
                .ifPresent(System.out::println);

        System.out.println("---------------");
        students.stream().max(Comparator.comparingInt(Student::getScore))
//                .collect(minBy(Comparator.comparingInt(Student::getScore)))
                .ifPresent(System.out::println);

        System.out.println("---------------");
        System.out.println(students.stream().collect(averagingInt(Student::getScore)));

        System.out.println("---------------");
        IntSummaryStatistics summaryStatistics = students.stream().collect(summarizingInt(Student::getScore));
        System.out.println(summaryStatistics);

        System.out.println("---------------");
        System.out.println(students.stream().map(Student::getName).collect(joining()));
        System.out.println(students.stream().map(Student::getName).collect(joining(", ")));
        System.out.println(students.stream().map(Student::getName).collect(joining(", ","<begin> ", " <end>")));

        System.out.println("---------------");
        //返回结果 command + option + v
        Map<Integer, Map<String, List<Student>>> collect = students.stream().collect(groupingBy(Student::getScore, groupingBy(Student::getName)));
        System.out.println(collect);

        Map<Boolean, List<Student>> collect1 = students.stream().collect(partitioningBy(student -> student.getScore() > 80));
        System.out.println(collect1);
        System.out.println("---------------");

        Map<Boolean, Map<Boolean, List<Student>>> collect2 = students.stream()
                                                                     .collect(partitioningBy(student -> student.getScore() > 80,
                                                                             partitioningBy(student -> student.getScore() > 90)));
        System.out.println(collect2);
        System.out.println("---------------");


        Map<Boolean, Long> map4 = students.stream().collect(partitioningBy(student -> student.getScore() > 80, counting()));
        System.out.println(map4);
        System.out.println("---------------");

        //获取学生的最低分
        Map<String, Student> collect3 = students.stream().collect(groupingBy(Student::getName, collectingAndThen(minBy(Comparator.comparingInt(Student::getScore)), Optional::get)));
        System.out.println(collect3);

    }
}
